package com.bite.order.controller;


import com.bite.order.constant.Constants;
import com.bite.order.model.OrderInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

    @Resource(name = "returnRabbitTemplate")
    private RabbitTemplate returnRabbitTemplate;

    @RequestMapping("/createOrder1")
    public String createOrder1() {
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend(Constants.ORDER_DIRECT_EXCHANGE, Constants.ORDER_DIRECT_ROUTING_KEY, orderId);
        log.info("订单号：{}，已发送至MQ", orderId);
        return "下单成功！";
    }

    @RequestMapping("/createOrder2")
    public String createOrder2() {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(UUID.randomUUID().toString());
        orderInfo.setGoodsName("测试商品");
        rabbitTemplate.convertAndSend(Constants.ORDER_DIRECT_EXCHANGE, Constants.ORDER_DIRECT_ROUTING_KEY, orderInfo);
        log.info("订单：{}，已发送至MQ", orderInfo);
        return "下单成功！";
    }

    @RequestMapping("/createOrder3")
    public String createOrder3() {
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend(Constants.ORDER_DIRECT_EXCHANGE, "ack", orderId);
        log.info("订单号：{}，已发送至MQ", orderId);
        return "下单成功！";
    }

    @RequestMapping("/createOrder4")
    public String createOrder4() {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(UUID.randomUUID().toString());
        orderInfo.setGoodsName("测试商品");
        rabbitTemplate.convertAndSend(Constants.ORDER_DIRECT_EXCHANGE, "ack", orderInfo);
        log.info("订单：{}，已发送至MQ", orderInfo);
        return "下单成功！";
    }

    @RequestMapping("/createOrder5")
    public String createOrder5() {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(UUID.randomUUID().toString());
        orderInfo.setGoodsName("测试商品");
        //方式一：先创建消息，再发送
        Message message = new Message(orderInfo.toString().getBytes(), new MessageProperties());
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.ORDER_DIRECT_EXCHANGE, "ack", message);
        //方式二：使用lambda
        rabbitTemplate.convertAndSend(Constants.ORDER_DIRECT_EXCHANGE, "ack", new OrderInfo(), msg -> {
            msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            return msg;
        });

        log.info("订单：{}，已发送至MQ", orderInfo);
        return "下单成功！";
    }

    @RequestMapping("/confirm")
    public String confirm() {
        CorrelationData correlationData = new CorrelationData();
        confirmRabbitTemplate.convertAndSend(Constants.PUBLISHER_CONFIRM_EXCHANGE, Constants.PUBLISHER_CONFIRM_ROUTING_KEY, "hello confirm");
        return "ok";
    }

    @RequestMapping("/returns")
    public String returns() {
        returnRabbitTemplate.convertAndSend(Constants.PUBLISHER_CONFIRM_EXCHANGE, Constants.PUBLISHER_CONFIRM_ROUTING_KEY, "hello returns");
        return "ok";
    }

    @RequestMapping("/retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, Constants.RETRY_ROUTING_KEY, "hello retry");
        return "ok";
    }
}
